package com.shujichen.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * 时间工具类
 */
public class DateUtil {

    public static String formatDate(Date date) {
        if (date == null) return null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    /**
     * 格式化时间毫秒值为时间字符串
     *
     * @param durationMillis 持续时间毫秒值
     * @return 格式化后的持续时间字符串
     */
    public static String formatDuration(long durationMillis) {
        // 如果持续时间为0毫秒，则返回"0秒/--"
        if (durationMillis == 0) {
            return "0秒";
        }

        if (durationMillis <= 1000) {
            return durationMillis + "毫秒";
        }

        // 定义一天的毫秒数和最大值
        long oneDayMillis = TimeUnit.DAYS.toMillis(1);
        int maxHours = 23;
        int maxMinutes = 59;
        int maxSeconds = 59;

        // 如果持续时间大于或等于1天，则返回"超过一天"
        if (durationMillis >= oneDayMillis) {
            return "超过一天";
        }

        // 从持续时间的毫秒数计算小时、分钟和秒
        long hours = TimeUnit.MILLISECONDS.toHours(durationMillis);
        long minutes = TimeUnit.MILLISECONDS.toMinutes(durationMillis - TimeUnit.HOURS.toMillis(hours));
        long seconds = TimeUnit.MILLISECONDS.toSeconds(durationMillis - TimeUnit.HOURS.toMillis(hours) - TimeUnit.MINUTES.toMillis(minutes));

        // 修正小时、分钟和秒的最大值
        hours = Math.min(hours, maxHours);
        minutes = Math.min(minutes, maxMinutes);
        seconds = Math.min(seconds, maxSeconds);

        // 格式化持续时间字符串
        StringBuilder formattedDuration = new StringBuilder();
        if (hours > 0) {
            formattedDuration.append(String.format("%02d", hours)).append("小时");
        }
        if (minutes > 0 || formattedDuration.length() > 0) {
            formattedDuration.append(String.format("%02d", minutes)).append("分钟");
        }
        if (seconds > 0 || formattedDuration.length() == 0) {
            formattedDuration.append(String.format("%02d", seconds)).append("秒");
        }

        // 返回格式化后的持续时间字符串
        return formattedDuration.toString();
    }

    /**
     * 计算时间差
     *
     * @param endTime   最后时间
     * @param startTime 开始时间
     * @return 时间差（天/小时/分钟）
     */
    public static String timeDistance(Date endTime, Date startTime) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endTime.getTime() - startTime.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return day + "天" + hour + "小时" + min + "分钟";
    }

    /**
     * 将毫秒值转换为时间字符串
     *
     * @param milliseconds 毫秒值
     * @return 时间字符串
     */
    public static String convertMillisecondsToTime(long milliseconds) {
        int hours = (int) (milliseconds / (60 * 60 * 1000));
        int minutes = (int) ((milliseconds % (60 * 60 * 1000)) / (60 * 1000));
        int seconds = (int) ((milliseconds % (60 * 1000)) / 1000);
        return String.format("%02d:%02d:%02d", hours, minutes, seconds);
    }

}
